#include <iostream>
using namespace std;
#include <sys/types.h>
#include <dirent.h>
#include <string.h>

void openDir(const char * pathname)
{
    /* 打开文件夹 */
    DIR * dir = opendir(pathname);
    if (dir == nullptr) 
    {
        cout << "无法打开目录: " << pathname << endl;
        return;
    }

    struct dirent * pdient;
    while((pdient = readdir(dir)) != NULL)
    {
        /* 排除 . 和 .. */
        if (strcmp(pdient->d_name, ".") == 0 || strcmp(pdient->d_name, "..") == 0) 
        {
            continue;
        }
        
        if((int)(pdient->d_type) == 8)
        {/* 普通文件 */
            const char * filename = pdient->d_name;
            if (strstr(filename, ".mp3") != nullptr)    //看普通文件中是否含有MP3文件
            {
                cout << "mp3: " << filename << endl;    
            }
        }
        else if((int)(pdient->d_type) == 4)
        {/* 目录 */
            string nextPath = string(pathname) + "/" + pdient->d_name;  //为遍历文件夹做准备
            openDir(nextPath.c_str());                  // 递归，遍历文件夹
        }
    }

    /* 关闭文件夹 */
    closedir(dir);


}

int main()
{
    const char * m_pathname =  "/home/第二阶段/267System-Call/FileIO";
    openDir(m_pathname);
    

    //cout <<"d_type: " << (int)pdient->d_type << " d_name " << pdient->d_name << endl;


    

    return 0;
}